class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            var key = e.keyCode;
            if (key === 13) {
                _Instance.FetchForPage(1);
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        item['YearValue'] = parseInt($.trim($("#YearOfBill").val()));
        item['MonthValue'] = parseInt($.trim($("#MonthOfBill").val()));
        item['SupplierName'] = $.trim($("#SupplierNameFilterValue").val());
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    EmptyTableContent() {
        $("#tab_data01 tbody").empty();
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let content_01 = '';
        this.rows = result.length;
        for (let i = 0; i < result.length; i++) {
            content_01 += '<tr>';
            content_01 += '<td style="vertical-align:middle;">' + formatNullData(result[i].pono) + '</td>';
            content_01 += '<td style="vertical-align:middle; text-align:left;">' + formatNullData(result[i].styleno) + '</td>';
            content_01 += '<td style="vertical-align:middle;">' + formatNullData(result[i].suppliername) + '</td>';
            content_01 += '<td rowspan="' + result[i].rows + '" style="vertical-align:middle; text-align:right;">' + formatNullData(result[i].payabledamount) + '</td>';
            content_01 += '<td rowspan="' + result[i].rows + '" style="vertical-align:middle; text-align:center;">' + formatNullData(result[i].days) + '</td>';
            content_01 += '<td rowspan="' + result[i].rows + '" style="vertical-align:middle; text-align:right;">' + formatNullData(result[i].cutpayabledamount) + '</td>';
            content_01 += '</tr>';
            $("#tab_data01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tab_data01");

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $('#loading').hide();
    }
    //《按钮》搜索
    DoSearch() {
        this.FetchForPage(1);
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 127 - 25) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    TableToExcel(table, sheetname, filename) {
        let uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)));
                },
                format = function (s, c) {
                    return s.replace(/{(\w+)}/g,
                            function (m, p) {
                                return c[p];
                            });
                };
        if (!table.nodeType)
            table = document.getElementById(table);
        let ctx = {worksheet: sheetname || 'Worksheet', table: $("#tab_head01").html() + table.innerHTML};
        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = filename; // 这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了
        document.getElementById("dlink").click();
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮
    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/financial/report/late_arrival_cut_payabled_amount/fetch.html",
            "");

    // 设置初始默认值
    let _currentDate = new Date();
    $("#YearOfBill").val(_currentDate.getFullYear());
    $("#MonthOfBill").val(_currentDate.getMonth() + 1);

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
